#include <QVector3D>
#include <QVariant>
#include <QMetaProperty>
#include <QQmlContext>
#include <QtQml>
#include <QTemporaryDir>
#include <QQmlFile>

#include "AnimMontage_p.h"
#include "AnimMontageUtils.h"
#include "AnimSource.h"

AnimSource::AnimSource():_P(new AnimSourcePrivate())
{

}

AnimSource::~AnimSource()
{
    delete _P;
}

QUrl AnimSource::getSource() const
{
    return source;
}

void AnimSource::setSource(QUrl newSource)
{
    if (source == newSource)
        return;
    source = newSource;
    update();
    emit sourceChanged();
}

static QSSGAssetImportManager::ImportState importScene(QUrl resolvedUrl,QSSGSceneDesc::Scene& scene,QString& error){
    QSSGAssetImportManager importManager;
    QSSGAssetImportManager::ImportState result = {};
    if(resolvedUrl.scheme() == "qrc")
    {
        auto qrcFile = QQmlFile::urlToLocalFileOrQrc(resolvedUrl);
        QFileInfo fileInfo(qrcFile);
        QString fileName = fileInfo.fileName();
        QFile file(qrcFile);
        file.open(QIODevice::ReadOnly);
        QTemporaryDir dir;
        QString tempPath = dir.path() + "/" + fileName;
        QFile tempFile(tempPath);
        tempFile.open(QIODevice::Truncate | QIODevice::WriteOnly);
        tempFile.write(file.readAll());
        resolvedUrl = QUrl::fromLocalFile(tempPath);
        result = importManager.importFile(resolvedUrl, scene, &error);
    }
    else if(resolvedUrl.scheme() == "file")
    {
       result = importManager.importFile(resolvedUrl, scene, &error);
    }

    return result;
}

static void getBasePose(Pose& output,QSSGSceneDesc::Node* node)
{
    //动画仅记录骨骼的信息
    QByteArray name = node->name;
    auto& target = output[name];
    for(const auto &i:node->properties)
    {
       if(i->name == QStringLiteral("position"))
       {
           target.position = i->value.value<QVector3D>();
       }
       else if(i->name == QStringLiteral("rotation"))
       {
           target.rotation = i->value.value<QQuaternion>();
       }
       else if(i->name == QStringLiteral("scale"))
       {
           target.scale = i->value.value<QVector3D>();
       }
    }

    for(const auto &i:node->children)
    {
        getBasePose(output,i);
    }
}

void AnimSource::update()
{
    _P->cleanData();

    const QQmlContext *context = qmlContext(this);
    auto resolvedUrl = (context ? context->resolvedUrl(source) : source);
    QString error(QStringLiteral("Unknown error"));
    _P->scene = new QSSGSceneDesc::Scene();

    auto result = importScene(resolvedUrl, *_P->scene, error);

    if(result != QSSGAssetImportManager::ImportState::Success)
    {
        qDebug()<<error;
        delete _P->scene;
        _P->scene = nullptr;
        return;
    }

    //先记录初始姿态
    getBasePose(_P->baseEntrys,_P->scene->root);
    setCount(_P->scene->animations.length());
}

int AnimSource::count() const
{
    return m_count;
}

void AnimSource::setCount(int newCount)
{
    if (m_count == newCount)
        return;
    m_count = newCount;
    emit countChanged();
}
